<!DOCTYPE html>
<html>
<head><title>Game Management</title>
<script src="interaction.js" type="text/JavaScript"></script>

<!--Ajax functions-->
<script type="text/javascript">
function showgame(str)
{
	
if (str=="")
  {
  document.getElementById("games_view").innerHTML="";
  return;
  }
   
if (window.XMLHttpRequest)
  {
	  
  xmlhttp=new XMLHttpRequest();
 
	}

xmlhttp.onreadystatechange=function()
  {
	  
  if (xmlhttp.status==200 && xmlhttp.readyState==4 ) //while connection is established and loading data is done
    {
		//document.write("xmlhttp.responseText"); 
    document.getElementById("games_view").innerHTML=xmlhttp.responseText;
    
    }
  }
xmlhttp.open("GET","Ajax_page.php?games="+str,true);
xmlhttp.send();
}
</script>
</head>




<body>
<fieldset>
<legend> Game Management</legend>


<?php include("../db.php");
session_start();
check_inactive();
authenticate_admin();
/* search bar for games by time/ gameid/ */
$db=pg_connect("host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p");
$userid=$_SESSION['userid'];

if(!$db)
{
echo "database connection failed";
exit;
}
if ($db)
{

operation_handler();

$sports=pg_query("SELECT * FROM Sports");/*add, delete and update a game type (insert into sports table)*/


//$$$$$$$$$$$$$$$$$$
// Add a sport
//$$$$$$$$$$$$$$$$$$
echo "<form name='add_f' action='Game_Management.php' method='post' >";
echo "Add a sport <input type='text' name='sportname_a' id='sportname_a'>";
echo "<br/>Enter Max Players Allowed in the game: <input type='text' name='Max_Players_a' />";
echo "<br/>Enter the duration time for this type of sport: <input type='text' name='duration_a' />";
echo "<input type='hidden' name='add_new_sport' value='adding'>";
echo "<input type='submit' value='add'>";
echo "</form>";

//$$$$$$$$$$$$$$$$$
//delete a sport
//$$$$$$$$$$$$$$$$$$
echo "<form action='Game_Management.php' method='post'>";
echo "Delete a sport:";

echo "<select name='sid_d'>";




while($sport_buff=pg_fetch_assoc($sports))
{
echo "<option value='".$sport_buff['sid']."'>" ;
//echo ": ";
echo $sport_buff["sport_name"];
echo "</option>";
}
echo "</select>";
echo "<input type='submit' value='delete' />";
echo "</form>";

//$$$$$$$$$$$$$$$$$$$$$$$$$
//update a game
//$$$$$$$$$$$$$$$$$$$$$$$$$
$sports_u=pg_query("SELECT * FROM Sports");
echo "<form action='Game_Management.php' method='post'>";
echo "Update ";

echo "<select name='sid_u'>";

while($sport_buff=pg_fetch_assoc($sports_u))
{
echo "<option value='".$sport_buff['sid']."'>";
//echo ": ";
echo $sport_buff["sport_name"];
echo "</option>";
}

echo "</select>";
echo "<br/>Enter Max Players Allowed in the game: <input type='text' name='Max_Players_u' value='default' />";
echo "<br/>Enter the duration time for this type of sport: <input type='text' name='duration_u' value='default' />";

echo "<input type='submit' value='update'>";
echo "</form>";



}


function add_sports()
{
	$a_sport=$_POST['sportname_a'];
	$a_maxP=$_POST['Max_Players_a'];
	$a_duration=$_POST['duration_a'];
	//echo "Sport: $a_sport Max Players: $a_maxP Duration:: $a_duration<br/>";
//	echo "<p>adding a game</p>";
	//check if the game you are adding already exists in the database
	$sports_a=pg_query("SELECT * FROM Sports");//list all the sports in database
	$case=1;
	if($case==1)
	{
	while($buff=pg_fetch_assoc($sports_a)) //check if sport in database
	{
		//echo "checking conflicts with".$buff['sport_name'] ."<br/>";
		if ($a_sport==$buff) //sport found in database
		{
			alert_sport_exist();
			$case=2;
			break;
			}
		}
		echo "no conflict \n";
	}
	
	if ($case!=2) //sport not found in database
	{	
		//echo "adding in progress /n";
	$s_count=pg_query("SELECT COUNT(sid) FROM Sports");
	$count_sports=pg_fetch_assoc($s_count);
	//$sid=$count_sports['count']+1; // sid is total sid +1, a new sport type
	 //echo "<br/> count:". $count_sports['count'];
	
	if($a_maxP>=2 )
	{
		//echo "<br/> Addition in database.";
		$query = "INSERT INTO Sports VALUES (default, '".$a_sport."', '".$a_maxP."','".$a_duration."')";
		
		$add_query = pg_query($query);

	}
	else
	{ 
		//echo "Alert!!!";
		//alert_add_game();
		}
	}
	echo "Adding sport done!";
  }

function delete_sports()
{
		$d_sport=$_POST['sid_d'];
		//echo "In delete_sports(). sid: $d_sport<br/>";
		//$d2_sport=pg_fetch_assoc['d_sport'];
		//$d_sid=pg_query("SELECT sid FROM Sports WHERE sport_name=$d_sport");
		$delete=pg_query("DELETE FROM Sports WHERE sid=".$d_sport);
			

}

function update_sports() //need check if the submited value is nothing or spaces
{
	echo "updating sports";
	//$check=alert_sport_update_1();//return 0, cancelled, or 1, proceed.
	//if (check==0) exit; //update cancelled
	
		
	
	$u_sport=$_POST['sid_u']; //not for update, reference only
	$u_maxP=$_POST['Max_Players_u'];
	$u_duration=$_POST['duration_u'];
	
	//echo "sport sid: $u_sport, Max_player: $u_maxP, Duration: $u_duration <br/>";
	
	
	if($u_maxP=="default" && $u_duration!="default")
	{	
		//echo "updating duration.<br/>";
		$update_duration ="UPDATE Sports SET duration='".$u_duration."' WHERE sid=".$u_sport;
		pg_query($update_duration);
		//echo $update_duration;
		}
	else if($u_maxP!="default" && $u_duration=="default")
	{	
		//echo "updating max players <br/>";
		$update_maxP="UPDATE Sports SET max_players=".$u_maxP." WHERE sid=".$u_sport;
	
		pg_query($update_maxP);
		//echo $update_maxP;
		}
	else if ($u_maxP=="default" || $u_duration=="default")
	{
		//echo "nothing to update yo~~<br/>";
		exit;
		}
	else if ($u_maxP!="" && $u_maxP!=" " && $u_maxP>=2 && $u_duration>0 && $u_duration!="" && $u_duration!=" ")
	{
		//echo "updating both!<br/>";
		$update_both="UPDATE Sports SET max_players=".$u_maxP.", duration='".$u_duration."'WHERE sid=".$u_sport;
		 pg_query($update_both);
		// echo $update_both;                                 
		}
	/*else if ($u_maxP<2 ||$u_duration<=0)
	{
		alert_sport_update_2();
		}
	
	else 
	{
		alert_sport_update_3();
		}*/
}

function operation_handler()
{
	
	if ($_POST['add_new_sport'])
	{
	//echo "<p>add action triggered</p>";	
	add_sports();
	}

	if($_POST['sid_d'])
	{
		//echo "delete triggered <br/>";
	delete_sports();
	
	}

	if($_POST['sid_u'])
	{
		//echo "update a game <br/>";
	update_sports();
	
	}
	
	if($_POST['sport_sid_l'])
	{
		
		
		//list_games();
		//exit;
		}
	
}

?>



</fieldset>

<!--List Games by tye-->

<fieldset>
	<legend>Game View </legend> 
<?php

if($db)
{
	
$sports_l=pg_query("SELECT * FROM Sports order by sport_name");
echo "<form action=Game_Management.php method='post'>";
?>
<select name="sport_sid_l" onchange="showgame(this.value)">

<?php
echo "<option value='' >Pick a sport: </option>";
while($sport_buff=pg_fetch_assoc($sports_l))
{
echo "<option value=". $sport_buff['sid'].">";

echo $sport_buff["sport_name"];
echo "</option>";
}

echo "</select>";
//echo "<input type='submit' value='List games' />";

echo "</form>";
}

echo "<div id='games_view'><b></b></div>";



function list_games()
{
	//echo "listing games<br/>";
	$l_sport_sid=$_POST['sport_sid_l'];
	//$l2_sport_name=pg_fetch_assoc($l_sport_name);
	//echo "sport sid is ".$l_sport_sid."<br/>";
	
	$list="SELECT * FROM games WHERE sid=".$l_sport_sid;
	$l_games=pg_query($list);
	echo "Listing games: ".$l_sport_sid;
	echo "<table border='1'>";
	while ($l2_games=pg_fetch_assoc($l_games))
	{
		//echo "<br/>listing". $l2_sports['gameid']."<br/>";
		if($l2_games['sid']==$l_sport_sid)
		{
			
			echo "<tr><td>sid: ";
			echo $l2_games['sid'];
			echo "</td>";
			echo "<td>gameid: ". $l2_games['gameid'];
			echo "</td>";
			echo "<td>start date: ". $l2_games['start_date'];
			echo "</td>";
			echo "<td>start time: ". $l2_games['start_time'];
			echo "</td>";
			echo "</tr>";
			}
	}	
	
	echo "</table>";
	
	}
?>
	
	
</fieldset>

</body>

</html>
